package com.ay.selfservicemachine.base;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Window;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.content.PermissionChecker;
import androidx.core.view.WindowCompat;

import com.ay.selfservicemachine.R;
import com.ay.selfservicemachine.permission.HQWPermissionListener;
import com.ay.selfservicemachine.utils.HQWPermissionUtil;
import com.ay.selfservicemachine.utils.HQWWeightUtil;
import com.ay.selfservicemachine.utils.Logger;

import java.util.Map;


public abstract class HQWAppCompatActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        ActivityCollector.addActivity(this);
        initStatusNavigationBar();
    }

    /********************************************* 权限 ******************************************/

    public HQWPermissionListener hqwPermissionListener;

    /**
     * 判断是否已经获取多个权限
     *
     * @param permissions 需要判断的权限们
     */
    public boolean HQWHavePermissions(String[] permissions) {
        for (int i = 0; i < permissions.length; i++) {
            if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                return HQWPermissionUtil.havePermissionSpecial(this, permissions[i]);//进行特殊全是是否已经获取判断
            }
        }
        return true;
    }

    /**
     * 判断权限第一次申请是否被拒绝
     *
     * @param permissions 需要判断的权限们
     */
    public boolean HQWPermissionIsRefuse(String[] permissions) {
        for (int i = 0; i < permissions.length; i++) {
            if (shouldShowRequestPermissionRationale(permissions[i])) {
                return true;
            }
        }
        return false;
    }

    /**
     * HQW获取权限
     *
     * @param permissions           需要的权限
     * @param hqwPermissionListener 获取权限回调
     * @value 建议配合HQWPermissionUtil.PermissionDispose()使用
     */
    public void HQWPermissions(String[] permissions, HQWPermissionListener hqwPermissionListener) {
        this.hqwPermissionListener = hqwPermissionListener;
        resultLauncherpermission.launch(permissions);
    }

    ActivityResultLauncher<String[]> resultLauncherpermission = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {
        @Override
        public void onActivityResult(Map<String, Boolean> result) {
            boolean havePermission = true;
            for (Map.Entry<String, Boolean> maps : result.entrySet()) {
                if (maps.getValue() == false) {
//                    havePermission = false;
                    havePermission = HQWPermissionUtil.havePermissionSpecial(HQWAppCompatActivity.this, maps.getKey());
                }
            }
            if (havePermission) {
                hqwPermissionListener.onsucceed();
            } else {
                hqwPermissionListener.onfailure();
            }
        }
    });

    /**
     * HQW获取特殊权限
     *
     * @param permission            需要的权限
     * @param hqwPermissionListener 获取权限回调
     * @value 建议配合HQWPermissionUtil.PermissionDispose()使用
     */
    public void HQWPermissionSpecial(String permission, HQWPermissionListener hqwPermissionListener) {
        this.hqwPermissionListener = hqwPermissionListener;

        if (HQWPermissionUtil.havePermissionSpecial(this, permission)) {
            hqwPermissionListener.onsucceed();
        } else {
            lunchPermissionSpecial.launch(permission);
        }
    }
    /**
     * 特殊权限请求
     */
    ActivityResultLauncher lunchPermissionSpecial = registerForActivityResult(new ActivityResultContract<String, Boolean>() {
        String permissionSpecial;

        @Override
        public Boolean parseResult(int i, @Nullable Intent intent) {
            Logger.i("特殊权限请求", "返回了");
            return HQWPermissionUtil.havePermissionSpecial(HQWAppCompatActivity.this, permissionSpecial);
        }

        @NonNull
        @Override
        public Intent createIntent(@NonNull Context context, String permisssion) {
            permissionSpecial = permisssion;
            Logger.i("特殊权限请求", "发出请求");
            return HQWPermissionUtil.permissionSpecialAction(context, permisssion);
        }
    }, new ActivityResultCallback<Boolean>() {
        @Override
        public void onActivityResult(Boolean havePermission) {
            if (havePermission) {
                hqwPermissionListener.onsucceed();
            } else {
                hqwPermissionListener.onfailure();
            }
        }
    });


    /**
     * 检查那些权限缺失
     *
     * @param permissions
     * @return
     */
    public String getDeniedPermissionStr(String... permissions) {
        String lockPerStr = "";
        for (String per : permissions) {
            int result = PermissionChecker.checkSelfPermission(this, per);
            if (result != PermissionChecker.PERMISSION_GRANTED) {
                if (per.contains(".")) {
                    per = per.substring(per.lastIndexOf(".") + 1);
                }
                if (TextUtils.isEmpty(lockPerStr)) {
                    lockPerStr += per;
                } else {
                    lockPerStr += "," + per;
                }
            }
        }
        return lockPerStr;
    }

    /********************************************* 状态栏 ******************************************/
    protected abstract int statusNavigationBarType();
    private void initStatusNavigationBar() {
        switch (statusNavigationBarType()) {
            case 0:
                HQWsetStatusNavigationBar(getColor(R.color.bg_color_light),
                        true,
                        true,
                        getColor(R.color.white),
                        true,
                        true);
                break;
            case 1://全屏
                HQWsetStatusNavigationBar(getColor(R.color.white),
                        true,
                        false,
                        getColor(R.color.white),
                        true,
                        false);
                HQWWeightUtil.hideStatusBarText(this);
                break;
            case 2:
                HQWsetStatusNavigationBar(getColor(R.color.transparent),
                        true,
                        false,
                        getColor(R.color.white),
                        true,
                        true);
                break;
            case 3://LoginActivity
                HQWsetStatusNavigationBar(getColor(R.color.white),
                        true,
                        true,
                        getColor(R.color.white),
                        false,
                        false);
                break;
        }
    }

    public void HQWsetStatusNavigationBar(int statusBarColor, boolean statusBarTextColor, boolean isShowStatusBar, int navigationBarColor, boolean NavigationBarTextColor, boolean isShowNavigationBar) {
        Window window = getWindow();

        if (isShowStatusBar) {
            HQWWeightUtil.showSystemStatusBar(this);
        } else {
            HQWWeightUtil.hideSystemStatusBar(this);
        }
        if (isShowNavigationBar) {
            HQWWeightUtil.showSystemNavigationBar(this);
        } else {
            HQWWeightUtil.hideSystemNavigationBar(this);
        }
        window.setStatusBarColor(statusBarColor);
        window.setNavigationBarColor(navigationBarColor);
        WindowCompat.getInsetsController(window, window.getDecorView()).setAppearanceLightStatusBars(statusBarTextColor);
        WindowCompat.getInsetsController(window, window.getDecorView()).setAppearanceLightNavigationBars(NavigationBarTextColor);
    }

}
